﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.DynamicData;
using System.Web.UI;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using NotAClue.ComponentModel.DataAnnotations;
using System.Web.UI.WebControls;

namespace NotAClue.Web.DynamicData
{
    /// <summary>
    /// Add the option to the Default Auto Field Generator 
    /// to not add the (Style="white-space: nowrap;") to each field.
    /// </summary>
    public class NoWrapAutoFieldGenerator : DefaultAutoFieldGenerator
    {
        private Boolean _noWrap;

        /// <summary>
        /// Initializes a new instance of the <see cref="NoWrapAutoFieldGenerator"/> class.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <param name="enableNoWrap">if set to <c>true</c> [enable no wrap].</param>
        /// <remarks></remarks>
        public NoWrapAutoFieldGenerator(MetaTable table, Boolean enableNoWrap = false)
            : base(table)
        {
            _noWrap = enableNoWrap;
        }

        protected override DynamicField CreateField(MetaColumn column, ContainerType containerType, DataBoundControlMode mode)
        {
            DynamicField field = base.CreateField(column, containerType, mode);

            // override the wrap behavior
            // with the noWrap parameter
            field.ItemStyle.Wrap = !_noWrap;
            return field;
        }
    }
}